/*
 * createTime: 2020/2/26 15:12
 * description: 全局方法
 */

// 验证手机号
global.isPhone = function(phone) {
	if (typeof phone === 'string') {
		phone = parseInt(phone);
	}
	let reg = /^1(((5[0-35-9][0-9])|([34879][0-9]{2}))[0-9]{7})$/;
	return reg.test(phone);
};

// 校验电子邮箱
global.isEmail = function(str) {
	// eslint-disable-next-line
	let reg = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
	return reg.test(str);
};

// 截取url中search所带参数的值
export const getUrlParams = (global.getUrlParams = function(variable) {
	let reg = new RegExp('(^|&)' + variable + '=([^&]*)(&|$)');
	let r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return decodeURIComponent(r[2]);
	}
	return null;
});

// base64 转file类型
global.convertBase64UrlToBlob = function(urlData) {
	let bytes = window.atob(urlData); //去掉url的头，并转换为byte
	// 处理异常,将ascii码小于0的转换为大于0
	let ab = new ArrayBuffer(bytes.length);
	let ia = new Uint8Array(ab);
	for (let i = 0; i < bytes.length; i++) {
		ia[i] = bytes.charCodeAt(i);
	}
	return new Blob([ab], {
		type: 'image/png'
	});
};

// 四舍五入函数
global.numberRound = function(val) {
	if (!val) val = 0;
	val = Number(val);
	let numFlag = true;
	if (val < 0) {
		val = Math.abs(val);
		numFlag = false;
	}
	let times = Math.pow(10, 2);
	let res = val * times + 0.5;
	res = parseInt(res, 10) / times;
	if (!numFlag) res = -res;
	return res;
};

export const numberRound = global.numberRound;

// 是否是对象判断
global.isObject = function(value) {
	return Object.prototype.toString.call(value) === '[object Object]';
};
